<template>
	<div id="shellEditBox"></div>
</template>

<script lang="ts" setup name="ShellStudio">
import * as monaco from 'monaco-editor'
import { nextTick, ref, onBeforeUnmount, onMounted, inject } from 'vue'

import { ElMessage } from 'element-plus'
const text = ref('')
const language = ref('shell')
const editorTheme = ref('vs-dark')

//
// MonacoEditor start
//
onBeforeUnmount(() => {
	editor.dispose()
})

onMounted(() => {
	editorInit()
})

let editor = null
//从父组件获取当前每个tab对应的值和编辑的tab名称
// const editorValues = inject('editorValues')
// const editableTabsValue = inject('editableTabsValue')

const editorInit = () => {
	nextTick(() => {
		!editor
			? (editor = monaco.editor.create(document.getElementById('shellEditBox') as HTMLElement, {
					value: text.value, // 编辑器初始显示文字
					language: language.value, // 语言支持自行查阅demo
					automaticLayout: true, // 自适应布局
					theme: editorTheme.value, // 官方自带三种主题vs, hc-black, or vs-dark
					foldingStrategy: 'indentation',
					renderLineHighlight: 'all', // 行亮
					selectOnLineNumbers: true, // 显示行号
					minimap: {
						enabled: false
					},
					cursorStyle: 'line', //光标样式
					readOnly: false, // 只读
					fontSize: 16, // 字体大小
					autoIndent: true, //自动布局
					useTabStops: false,
					quickSuggestionsDelay: 100, //代码提示延时
					scrollBeyondLastLine: false, // 取消代码后面一大段空白
					overviewRulerBorder: false // 不要滚动条的边框
			  }))
			: editor.setValue('')
		editor.setValue('')
		// console.log(editor)
	})
}

const setEditorValue = (value: any) => {
	if (editor) {
		editor.setValue(value)
		if (value) {
			editor.trigger(null, 'editor.action.formatDocument')
		}
	}
}

const getEditorValue = () => {
	return editor.getValue()
}

/***
 * editor.setValue(newValue)

editor.getValue()

editor.onDidChangeModelContent((val)=>{ //监听值的变化  })

editor.getAction('editor.action.formatDocument').run()    //格式化代码

editor.dispose()    //销毁实例

editor.onDidChangeOptions　　//配置改变事件

editor.onDidChangeLanguage　　//语言改变事件
 */
defineExpose({
	setEditorValue,
	getEditorValue
})
</script>

<style scoped>
#shellEditBox {
	width: 100%;
	height: calc(100% - 20px);
}
</style>